library(ggplot2)
library(patchwork) 

setwd("~/Dropbox/WhiteRacialSocialization/Data/GoogleTrends")


# Loading and Cleaning ----------------------------------------------------
load("google_trends.rdata")

## Including events
events <- read.csv("blm events.csv")

# Formatting Date information
events$date <- as.character(events$date)
events$date <- as.Date(events$date, format = "%m/%d/%y")
events$month <- NA
events$month <- format(events$date, format = "%m")
events$year <- NA
events$year <- format(events$date, format = "%Y")

# Select plottables
events.sub <- subset(events, use == 1)

# Reformat Labels
events.sub$event.t <- NA
events.sub$event.t <- c("George\nZimmerman\nVerdict",
                        "Michael\nBrown\nshooting",
                        "Freddie\nGray\ndeath",
                        "Charleston\nChurch\nShooting",
                        "Philando\nCastile\nshooting",
                        "Colin Kaepernick\nKneels",
                        "Unite the Right\nRally",
                        "How to\nbe an\nAnti-Racist\nPublished",
                        "Ahmaud\nArbery\nkilled",
                        "Breonna\nTaylor\nkilled",
                        "George Floyd killed")
events.sub$date.c <- as.Date(events.sub$date)
events.sub$y <- 0
events.sub$yend <- c(21, 38, 24,  58, 
                     45, 65, 25, 20, 30, 60, 105)
events.sub$num <- 1:nrow(events.sub)


# Plotting ----------------------------------------------------------------
axis_text_x <- 10
axis_text_x_main <- 12
axis_text_y <- 12
axis_title <- 14
plot_title <- 14
line_size <- 1

plot_antiracism <- ggplot(subset(gtrend, word == sort(unique(gtrend$word))[1]), 
                          aes(x = date, y = popularity2)) +
  # Google search line
  geom_line(size = line_size, color="grey")  +
  theme_bw() +   
  labs(x = "", y = "Popularity", title = sort(unique(gtrend$word))[1]) +
  scale_y_continuous(breaks = seq(0, 115, by = 20),
                     minor_breaks = NULL) +
  scale_x_date(date_breaks = "5 months", date_labels = "%b %Y",
               limits = as.Date(c("2013-01-01", "2020-12-01")),
               minor_breaks = NULL) +
  theme(axis.text.x = element_text(angle = 90, size = axis_text_x, vjust = .5),
        axis.text.y = element_text(size = axis_text_y),
        axis.title = element_text(size = axis_title),
        plot.title = element_text(size = plot_title))

# BLM
plot_blm <- ggplot(subset(gtrend, word == sort(unique(gtrend$word))[2]), 
                   aes(x = date, y = popularity2)) +
  # segments for event lines
  geom_segment(data = events.sub,
               aes(x = date,
                   xend = date,
                   y = y,
                   yend = yend-4),
               linetype = "dashed", color = "darkorange1", inherit.aes = FALSE) +
  # Google search line
  geom_line(size = line_size, color="grey")  +
  # Text for Events
  geom_text(data = events.sub,
            aes(x = date.c,
                y = yend,
                label = event.t,
                lineheight = .8),
            size = 3,
            nudge_y = 3) +
  theme_bw() +   
  labs(x = "", y = "Popularity", title = sort(unique(gtrend$word))[2]) +
  scale_y_continuous(breaks = seq(0, 115, by = 20),
                     minor_breaks = NULL) +
  scale_x_date(date_breaks = "5 months", date_labels = "%b %Y",
               limits = as.Date(c("2013-01-01", "2020-12-01")),
               minor_breaks = NULL) +
  theme(axis.text.x = element_text(angle = 45, vjust = .5, size = axis_text_x_main),
        axis.text.y = element_text(size = axis_text_y),
        axis.title = element_text(size = axis_title),
        plot.title = element_text(size = plot_title))

# Talk
plot_talk <- ggplot(subset(gtrend, word == sort(unique(gtrend$word))[3]), 
                    aes(x = date, y = popularity2)) +
  # Google search line
  geom_line(size = line_size, color="grey")  +
  theme_bw() +   
  labs(x = "", y = "Popularity", title = sort(unique(gtrend$word))[3]) +
  scale_y_continuous(breaks = seq(0, 115, by = 20),
                     minor_breaks = NULL) +
  scale_x_date(date_breaks = "5 months", date_labels = "%b %Y",
               limits = as.Date(c("2013-01-01", "2020-12-01")),
               minor_breaks = NULL) +
  theme(axis.text.x = element_text(angle = 90, size = axis_text_x, vjust = .5),
        axis.text.y = element_text(size = axis_text_y),
        axis.title = element_text(size = axis_title),
        plot.title = element_text(size = plot_title))

# Racism
plot_racism <- ggplot(subset(gtrend, word == sort(unique(gtrend$word))[5]), 
                      aes(x = date, y = popularity2)) +
  # Google search line
  geom_line(size = line_size, color="grey")  +
  theme_bw() +   
  labs(x = "", y = "Popularity", title = sort(unique(gtrend$word))[5]) +
  scale_y_continuous(breaks = seq(0, 115, by = 20),
                     minor_breaks = NULL) +
  scale_x_date(date_breaks = "5 months", date_labels = "%b %Y",
               limits = as.Date(c("2013-01-01", "2020-12-01")),
               minor_breaks = NULL) +
  theme(axis.text.x = element_text(angle = 90, size = axis_text_x, vjust = .5),
        axis.text.y = element_text(size = axis_text_y),
        axis.title = element_text(size = axis_title),
        plot.title = element_text(size = plot_title))

# White Privilege
plot_priv <- ggplot(subset(gtrend, word == sort(unique(gtrend$word))[7]), 
                    aes(x = date, y = popularity2)) +
  # Google search line
  geom_line(size = line_size, color="grey")  +
  theme_bw() +   
  labs(x = "", y = "Popularity", title = sort(unique(gtrend$word))[7]) +
  scale_y_continuous(breaks = seq(0, 115, by = 20),
                     minor_breaks = NULL) +
  scale_x_date(date_breaks = "5 months", date_labels = "%b %Y",
               limits = as.Date(c("2013-01-01", "2020-12-01")),
               minor_breaks = NULL) +
  theme(axis.text.x = element_text(angle = 90, size = axis_text_x, vjust = .5),
        axis.text.y = element_text(size = axis_text_y),
        axis.title = element_text(size = axis_title),
        plot.title = element_text(size = plot_title))


# Riot
plot_riot <- ggplot(subset(gtrend, word == sort(unique(gtrend$word))[6]), 
                    aes(x = date, y = popularity2)) +
  # Google search line
  geom_line(size = line_size, color="grey")  +
  theme_bw() +   
  labs(x = "", y = "Popularity", title = sort(unique(gtrend$word))[6]) +
  scale_y_continuous(breaks = seq(0, 115, by = 20),
                     minor_breaks = NULL) +
  scale_x_date(date_breaks = "5 months", date_labels = "%b %Y",
               limits = as.Date(c("2013-01-01", "2020-12-01")),
               minor_breaks = NULL) +
  theme(axis.text.x = element_text(angle = 90, size = axis_text_x, vjust = .5),
        axis.text.y = element_text(size = axis_text_y),
        axis.title = element_text(size = axis_title),
        plot.title = element_text(size = plot_title))

# Loot
plot_loot <- ggplot(subset(gtrend, word == sort(unique(gtrend$word))[4]), 
                    aes(x = date, y = popularity2)) +
  # Google search line
  geom_line(size = line_size, color="grey")  +
  theme_bw() +   
  labs(x = "", y = "Popularity", title = sort(unique(gtrend$word))[4]) +
  scale_y_continuous(breaks = seq(0, 115, by = 20),
                     minor_breaks = NULL) +
  scale_x_date(date_breaks = "5 months", date_labels = "%b %Y",
               limits = as.Date(c("2013-01-01", "2020-12-01")),
               minor_breaks = NULL) +
  theme(axis.text.x = element_text(angle = 90, size = axis_text_x, vjust = .5),
        axis.text.y = element_text(size = axis_text_y),
        axis.title = element_text(size = axis_title),
        plot.title = element_text(size = plot_title))


(plot_blm)/(plot_racism  + plot_antiracism + plot_priv)/(plot_loot+ plot_riot + plot_talk )
# ggsave("fig1.pdf", width = 10, height = 10)

